package com.neulogistics.contrib.usercenter.api.application.command.handler;

import cn.hutool.crypto.digest.MD5;
import com.blackto.lib.mediat.unicast.IRequestHandler;
import com.neulogistics.contrib.usercenter.api.application.command.LoginCommand;
import com.neulogistics.contrib.usercenter.api.application.command.LogoutCommand;
import com.neulogistics.contrib.usercenter.api.application.viewmodel.LoginUserViewModel;
import com.neulogistics.contrib.usercenter.domain.model.aggregate.User;
import com.neulogistics.contrib.usercenter.domain.repository.IUserRepository;
import com.neulogistics.infrastructure.cache.idservice.RedisIdUtil;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import com.neulogistics.infrastructure.common.token.JwtTokenUtil;
import com.neulogistics.infrastructure.common.token.LoginUser;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

@Component
public class LogoutCommandHandler implements IRequestHandler<LogoutCommand, BaseResponse> {
    @Resource
    private IUserRepository userRepository;

    @Resource
    RedisIdUtil redisIdUtil;

    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Override
    public BaseResponse handle(LogoutCommand command) {
        User user = userRepository.findById(command.userId);
        // 清除缓存
        String refreshTokenKey = JwtTokenUtil.getRefreshTokenKey(user.getUserId());
        stringRedisTemplate.delete(refreshTokenKey);

        return ResultUtils.success(null);
    }
}
